#include "control.h"

/***********************************全局变量***********************************/
uint8_t  timeout = 0;                                                           //设置参数页面超时标志位
uint16_t timecnt = 0;                                                           //向上计数
uint16_t LedStatic = 0;                                                         //LED状态寄存器
uint8_t  heating = 0;                                                           //风机加热标志位 bit0:加热 bit1:风机延时  bit7:加热状态标志位

/***********************************私有变量***********************************/
static uint8_t list_num = 0;                                                    //设置参数列表序号
static uint8_t keystate = 0;
static uint8_t st_state = 0;

/******************************************************************************/


/***********************************************
*函数名称：Task_Key_Scan
*功    能：按键扫描
*入口参数：无
*返 回 值：无
*备    注：
************************************************/
void Task_Key_Scan(void)
{
	uint8_t i;
	static uint8_t blower = 0;
	static uint8_t locrom = 0;
	uint32_t keyval;
	uint32_t temp;
	keyval = key_static;

	if(keyval == KEY_BLOWER_LONG){                                                //停止
		if(heating == 0 && P_Buf[JST].newdata ==0 && P_Buf[JSQ].newdata ==0){
			BlowerOff();                                                              //关闭风机
			
		}else if(heating & 0x80 && P_Buf[JST].newdata ==0 && P_Buf[JSQ].newdata ==0){
			HeatingOff();                                                             //加热模式停止
			heating = 0x02;
			led_toggle(1,1);                                                          //风机延时指示灯亮
			timecnt = P_Buf[FST0].newdata;
			
		}else if(P_Buf[JST].newdata ==1 || P_Buf[JSQ].newdata ==1){
			st_state = 0;
			P_Buf[JST].newdata =0;                                                    //定时停止模式关
			P_Buf[JSQ].newdata =0;                                                    //定时启动模式关
			lv_led_off(pic[3]);                                                       //定时停止指示灯灭
			lv_led_off(pic[4]);                                                       //定时启动指示灯灭
			if(heating & 0x80){
				HeatingOff();                                                           //加热模式停止
				led_toggle(1,1);                                                        //风机延时指示灯亮
				heating = 0x02;
				timecnt = P_Buf[FST0].newdata;
				
			}else{
				time_init_disp(variable_label[3]);                                      //计时时间初始界面
				
			}
		}
	}
	if(keyval == KEY_LOC_REM_SHORT){                                              //本地/远程
		lv_led_toggle(pic[5]);
		lv_led_toggle(pic[2]);
		if(locrom == 0){locrom =1 ;}
		else {locrom = 0;}
	}
	if(keyval == KEY_RESET_TIME_LONG){                                            //计时复位
		TimingReset();                                                              //复位
		
	}
	if(keyval == KEY_BLOWER_SHORT && P_Buf[JST].newdata ==0 && P_Buf[JSQ].newdata ==0){//送风
		BlowerOn();                                                                 //开始吹风
		blower = 1;
		
	}
	if(keyval == KEY_HEATING_SHORT && blower ==1 ){                               //加热
		HeatingOn();                                                                //开始加热
		blower = 0;
		heating = 0x80;
		timecnt = 0;
	}
	switch(keystate)
	{
		case 0:
			timeout = 0;
			if(keyval == KEY_SET_SHORT){                                              //设定参数界面1
				keystate = 1;
				table_page_switch(1);                                                   //切换到界面1
				lv_event_send(table_page_spinbox, LV_EVENT_KEY, NULL);                  //向列表发送事件
			}
			if(keyval == KEY_SET_LONG){                                               //设定参数界面2
			  keystate = 2;
			  table_page_switch(2);                                                   //切换到界面2
				lv_send_keyevent(list_num);
				
			}
			if(keyval == (KEY_LEFT_SHORT | KEY_SET_SHORT)){                           //设定参数界面3
			  keystate = 3;
			  table_page_switch(3);                                                   //切换到界面3
				lv_send_keyevent1(list_num);
				
			}
			for(i = 1;i < 30;i++){
				P_Buf[i].datacache = P_Buf[i].newdata;
				
			}
		  break;
			
		case 1:
			TimeOutFlag();                                                            //超时返回主界面
			if(keyval == KEY_UP_SHORT){
				lv_spinbox_increment(table_page_spinbox);                               //参数值+1
				
			}if(keyval == KEY_DOWN_SHORT){
				lv_spinbox_decrement(table_page_spinbox);                               //参数值-1
				
			}
	    if(keyval == KEY_LEFT_SHORT){
				lv_spinbox_step_prev(table_page_spinbox);                               //将设置光标左移一位
				
			}	    
			if(keyval == KEY_RIGHT_SHORT){
				lv_spinbox_step_next(table_page_spinbox);                               //将设置光标右移一位
				
			}
			if(keyval == KEY_SET_SHORT){
				keystate = 0;
				list_num = 0;
				temp=lv_spinbox_get_value(table_page_spinbox);                          //获取设定的温度值
				P_Buf[SP].newdata = temp;
				show_num(variable_label[2],temp);                                       //设定温度
				ParameterSave();                                                        //保存设定参数
				table_page_switch(0);
				
			}
	  break;

		case 2:
			TimeOutFlag();                                                            //超时返回主界面
			if(keyval == KEY_UP_SHORT){
				lv_spinbox_increment(spinbox1);                                         //参数值+1
				P_Buf[list_num+1].datacache = lv_spinbox_get_value(spinbox1);           //获取设置的参数
				
			}
			if(keyval == KEY_DOWN_SHORT){  
				lv_spinbox_decrement(spinbox1);                                         //参数值-1
				P_Buf[list_num+1].datacache = lv_spinbox_get_value(spinbox1);           //获取设置的参数
				
			}
	    if(keyval == KEY_LEFT_SHORT){
				lv_spinbox_step_prev(spinbox1);                                         //将设置光标左移一位
				
			}
      if(keyval == KEY_RIGHT_SHORT){
				lv_spinbox_step_next(spinbox1);                                         //将设置光标右移一位
				
			}
			if(keyval == KEY_SET_SHORT){
				if(list_num == 25){list_num = 0;}
				else {list_num++;}
				lv_send_keyevent(list_num);
				
			}
			if(keyval == KEY_SET_LONG){
				keystate = 0;
				list_num = 0;
				table_page_switch(0);
				for(i = 1;i < 27;i++){
					P_Buf[i].newdata = P_Buf[i].datacache;
					
				}
				ParameterSave();                                                        //保存设定参数
			}
			break;
		case 3:
			TimeOutFlag();                                                            //超时返回主界面
			if(keyval == KEY_UP_SHORT){
				lv_spinbox_increment(spinbox1);                                         //参数值+1
				P_Buf[list_num+27].datacache = lv_spinbox_get_value(spinbox1);           //获取设置的参数
				
			}
			if(keyval == KEY_DOWN_SHORT){  
				lv_spinbox_decrement(spinbox1);                                         //参数值-1
				P_Buf[list_num+27].datacache = lv_spinbox_get_value(spinbox1);           //获取设置的参数
				
			}
	    if(keyval == KEY_LEFT_SHORT){
				lv_spinbox_step_prev(spinbox1);                                         //将设置光标左移一位
				
			}
      if(keyval == KEY_RIGHT_SHORT){
				lv_spinbox_step_next(spinbox1);                                         //将设置光标右移一位
				
			}
			if(keyval == KEY_SET_SHORT){
				if(list_num == 2){list_num = 0;}
				else {list_num++;}
				lv_send_keyevent1(list_num);
				
			}
			if(keyval == KEY_SET_LONG){
				keystate = 0;
				table_page_switch(0);
				list_num = 0;
				for(i = 27;i < 30;i++){
					P_Buf[i].newdata = P_Buf[i].datacache;
					
				}
				ParameterSave();                                                        //保存设定参数
			}
			break;
	}
	key_static = 0;                                                               //按键状态寄存器清零
	
	HeatingAndBlower();                                                           //风机和加热控制
	AlarmJudgement();                                                             //报警控制
	
	Modbus_Tick();                                                                //Modbus处理函数
	BaudSet();                                                                    //设置波特率
		
}

/***********************************************
*函数名称：HeatingAndBlower
*功    能：风机和加热控制
*入口参数：无
*返 回 值：无
*备    注：
************************************************/
static void HeatingAndBlower(void)
{
	if(P_Buf[JSQ].newdata == 0 && P_Buf[JST].newdata == 0 ){                      //定时功能未启动
		if(heating & 0x80){
			show_num_time(variable_label[3],timecnt);                                 //加热计时时间
			
		}
	}
	if(heating == 0x02){
		show_num_time(variable_label[3],timecnt);                                   //风机延时计时时间
		if(timecnt == 0){
			BlowerOff();                                                              //停止吹风
			time_init_disp(variable_label[3]);                                        //计时时间初始界面
			
		}
	}else{
		switch(st_state)
		{
			case 0:
				if(P_Buf[JSQ].newdata == 1 && P_Buf[JST].newdata == 1){                 //定时启动开，定时停止开
					if(heating & 0x80){
						timecnt = ReadTime(P_Buf[STOP].newdata);
						lv_led_on(pic[3]);                                                  //定时停止指示灯亮
					}else{
						timecnt = ReadTime(P_Buf[STAR].newdata);
						lv_led_on(pic[4]);                                                  //定时启动指示灯亮
					}
					st_state = 1;
				}else if(P_Buf[JSQ].newdata == 1){                                      //定时启动开，定时停止关
					timecnt = ReadTime(P_Buf[STAR].newdata);
					lv_led_on(pic[4]);                                                    //定时启动指示灯亮
					st_state = 2;
					
				}else if(P_Buf[JST].newdata == 1 ){                                     //定时启动关，定时停止开
					timecnt = ReadTime(P_Buf[STOP].newdata);
					lv_led_on(pic[3]);                                                    //定时停止指示灯亮
					st_state = 2;
					
				}
			break;

			case 1:
				show_num_time(variable_label[3],timecnt);                               //定时停止和启动计时开始
			  if(timecnt == 0){
					if(heating & 0x80){
						HeatingOff();                                                       //停止加热
						heating = 0x02;
						led_toggle(1,1);                                                    //风机延时指示灯亮
						lv_led_off(pic[3]);                                                 //定时停止指示灯灭
						timecnt = P_Buf[FST0].newdata;
						
					}else{
						heating = 0x80;
						BlowerOn();                                                         //风机打开
						HeatingOn();                                                        //加热打开
						lv_led_off(pic[4]);                                                 //定时启动指示灯灭
						
					}
					st_state = 0;
				}
			break;
				
			case 2:
				if((heating & 0x80) && (P_Buf[JST].newdata == 1)){
					show_num_time(variable_label[3],timecnt);                             //定时停止计时开始
					if(timecnt == 0){
						st_state = 0;
						P_Buf[JST].newdata = 0;
						lv_led_off(pic[3]);                                                 //定时停止指示灯灭
						HeatingOff();                                                       //停止加热
						heating = 0x02;
						led_toggle(1,1);                                                    //风机延时指示灯亮
						timecnt = P_Buf[FST0].newdata;
					}
				}else if(P_Buf[JSQ].newdata == 1 && heating == 0x00){
					show_num_time(variable_label[3],timecnt);                             //定时启动计时开始
					if(timecnt == 0){
						st_state = 0;
						P_Buf[JSQ].newdata = 0;
						lv_led_off(pic[4]);                                                 //定时启动指示灯灭
						heating = 0x80;
						BlowerOn();                                                         //风机打开
						HeatingOn();                                                        //加热打开
					}
				}
			break;
		}
	}
}

/***********************************************
*函数名称：AlarmJudgement
*功    能：参数判断
*入口参数：无
*返 回 值：无
*备    注：
************************************************/
static void AlarmJudgement(void)
{
	static uint8_t lowtemp = 0;
	uint8_t i;
	if(heating & 0x80){
    if((pv_temp < (P_Buf[ALH1].newdata + P_Buf[SP].newdata))
				&& (iv_temp < P_Buf[ALH2].newdata)){
			
			HeatingOn();                                                              //打开加热
			if(pv_temp < (P_Buf[ALH1].newdata + P_Buf[SP].newdata)){
				P_Buf[ALL1].newdata = 0;
				led_toggle(4,0);                                                        //热风超温报警灭
				OutPutControl(RELAY0,0);                                                //热风超温继电器断开
			}
			if(iv_temp < P_Buf[ALH2].newdata){
				P_Buf[ALL2].newdata = 0;
				led_toggle(10,0);                                                       //进风超温报警灭
				OutPutControl(RELAY1,0);                                                //进风超温继电器断开
				
			}
    }else if((pv_temp > (P_Buf[ALH1].newdata + P_Buf[SP].newdata))
			||(iv_temp > P_Buf[ALH2].newdata)){
				
			HeatingOff();                                                             //停止加热
			if(pv_temp > (P_Buf[ALH1].newdata + P_Buf[SP].newdata)){                  //ALH1报警控制
				P_Buf[ALL1].newdata = 1;
				OutPutControl(RELAY0,1);                                                //热风超温继电器闭合
				led_toggle(4,1);                                                        //热风超温报警亮
				
			}
		  if(iv_temp > P_Buf[ALH2].newdata){                                        //ALH2报警控制
				P_Buf[ALL2].newdata = 1;
				led_toggle(10,1);                                                       //进风超温报警亮
				OutPutControl(RELAY1,1);                                                //进风超温继电器闭合
			}
		}
		if(lowtemp == 0 && pv_temp < (P_Buf[SP].newdata - P_Buf[ALL].newdata)){
			lowtemp = 1;
			
		}else if(lowtemp == 1 && pv_temp > (P_Buf[SP].newdata - P_Buf[ALL].newdata)){
			lowtemp = 2;
			
		}else if(lowtemp == 2 && pv_temp < (P_Buf[SP].newdata - P_Buf[ALL].newdata)){
			lowtemp = 0;
			led_toggle(5,1);                                                          //低温报警亮
			lowtemp = 0;
			OutPutControl(RELAY2,1);                                                  //低温报警继电器闭合
			
		}else if(pv_temp > (P_Buf[SP].newdata - P_Buf[ALL].newdata)){
			led_toggle(5,0);                                                          //低温报警灭
			OutPutControl(RELAY2,0);                                                  //低温报警继电器断开
			
		}
		if(pv_temp > (P_Buf[FETC].newdata)){                                        //FETC报警控制
			led_toggle(7,1);                                                          //温度到达报警亮
			OutPutControl(RELAY3,1);                                                  //温度到达继电器闭合
			
		}else{
			led_toggle(7,0);                                                          //温度到达报警灭
			OutPutControl(RELAY3,0);                                                  //温度到达继电器断开
			
		}
	}else {
		lowtemp = 0;                                                                //停止加热后，标志位清零
		if(pv_temp < (P_Buf[ALH1].newdata + P_Buf[SP].newdata)){
			P_Buf[ALL1].newdata = 0;
			led_toggle(4,0);                                                          //热风超温报警灭
			OutPutControl(RELAY0,0);                                                  //热风超温继电器断开
			
		}
		if(iv_temp < P_Buf[ALH2].newdata){
			P_Buf[ALL2].newdata = 0;
			led_toggle(10,0);                                                         //进风超温报警灭
			OutPutControl(RELAY1,0);                                                  //进风超温继电器断开
			
		}
		if(pv_temp > (P_Buf[SP].newdata - P_Buf[ALL].newdata)){
				led_toggle(5,0);                                                        //低温报警灭
				OutPutControl(RELAY2,0);                                                //低温报警继电器断开
		}
		if(pv_temp < (P_Buf[FETC].newdata)){                                        //FETC报警控制
			led_toggle(7,0);                                                          //温度到达报警灭
			OutPutControl(RELAY3,0);                                                  //温度到达继电器断开
			
		}
		
	}
	LedSingleInput();                                                             //输入报警信号处理函数
	if(P_Buf[LOCK].newdata == 3 && P_Buf[INIT].newdata == 1){
		for(i=0;i<30;i++){
			P_Buf[i].newdata = P_Buf[i].initdata;
			
		}
	}
}

/***********************************************
*函数名称：TimingReset
*功    能：计时复位功能
*入口参数：无
*返 回 值：无
*备    注：
************************************************/
static void TimingReset(void)
{
  if((P_Buf[JSQ].newdata == 0x01) && (P_Buf[JST].newdata == 0x01)){
		if(heating & 0x80){
		  timecnt = ReadTime(P_Buf[STOP].newdata);
			
		}else {
		  timecnt = ReadTime(P_Buf[STAR].newdata);
			
		}
	}else if(P_Buf[JST].newdata == 0x01){
		timecnt = ReadTime(P_Buf[STOP].newdata);

	}else if(P_Buf[JSQ].newdata == 0x01){
		timecnt = ReadTime(P_Buf[STAR].newdata);

	}else if(heating == 0x02){
		timecnt = ReadTime(P_Buf[FST0].newdata);
		
	}else{
		timecnt = 0;
	}
}

/***********************************************
*函数名称：ReadTime
*功    能：将设置的时间转换成时间
*入口参数：设置的时间
*返 回 值：转换后的时间
*备    注：
************************************************/
static uint16_t ReadTime(uint16_t time)
{
	uint16_t hour,min;
	hour = time/100;
	min = time%100;
	return hour*60+min;
}

/***********************************************
*函数名称：HeatingOn
*功    能：开启加热
*入口参数：无
*返 回 值：无
*备    注：
************************************************/
static void HeatingOn(void)
{
	heating |= 0x01;
	lv_led_on(pic[1]);//加热指示灯亮
	 //开始加热
}

/***********************************************
*函数名称：HeatingOff
*功    能：关闭加热
*入口参数：无
*返 回 值：无
*备    注：
************************************************/
static void HeatingOff(void)
{
	heating &= ~0x01;
	lv_led_off(pic[1]);//加热指示灯灭
	 //停止加热
}

/***********************************************
*函数名称：BlowerOn
*功    能：打开风机
*入口参数：无
*返 回 值：无
*备    注：
************************************************/
static void BlowerOn(void)
{
	led_toggle(11,0);//运行准备指示灯灭
	lv_led_on(pic[0]);//送风指示灯亮
	 //开始吹风
}

/***********************************************
*函数名称：BlowerOff
*功    能：关闭风机
*入口参数：无
*返 回 值：无
*备    注：
************************************************/
static void BlowerOff(void)
{
	heating = 0;
	lv_led_off(pic[0]);//送风指示灯灭
	led_toggle(1,0);//风机延时指示灯灭
	 //停止吹风
	led_toggle(11,1);//运行准备指示灯亮
}

/***********************************************
*函数名称：TimeOutFlag
*功    能：按键界面超时返回
*入口参数：无
*返 回 值：无
*备    注：
************************************************/
static void TimeOutFlag(void)
{
	if(key_static != 0){
		timeout = 0;
		
	}
	if(timeout>60){
		table_page_switch(0);
		keystate = 0;
		list_num = 0;
	}
}

/***********************************************
*函数名称：led_toggle
*功    能：设置指示灯状态并设置指示灯状态寄存器
*入口参数：lednum：指示灯序号
           toggle：指示灯状态  0熄灭  1点亮
*返 回 值：无
*备    注：
************************************************/
void led_toggle(uint8_t lednum,uint8_t toggle)
{
	switch(lednum){
		case  0:(toggle)?(lv_led_on(led[0]) ,LedStatic |= 0x0080):(lv_led_off(led[0]),LedStatic &= ~0x0080);break;
		case  1:(toggle)?(lv_led_on(led[1]) ,LedStatic |= 0x0002):(lv_led_off(led[1]),LedStatic &= ~0x0002);break;
		case  2:(toggle)?(lv_led_on(led[2]) ,LedStatic |= 0x0001):(lv_led_off(led[2]),LedStatic &= ~0x0001);break;
		case  3:(toggle)?(lv_led_on(led[3]) ,LedStatic |= 0x0010):(lv_led_off(led[3]),LedStatic &= ~0x0010);break;
		case  4:(toggle)?(lv_led_on(led[4]) ,LedStatic |= 0x0100):(lv_led_off(led[4]),LedStatic &= ~0x0100);break;
		case  5:(toggle)?(lv_led_on(led[5]) ,LedStatic |= 0x0800):(lv_led_off(led[5]),LedStatic &= ~0x0800);break;
		case  6:(toggle)?(lv_led_on(led[6]) ,LedStatic |= 0x0004):(lv_led_off(led[6]),LedStatic &= ~0x0004);break;
		case  7:(toggle)?(lv_led_on(led[7]) ,LedStatic |= 0x0040):(lv_led_off(led[7]),LedStatic &= ~0x0040);break;
		case  8:(toggle)?(lv_led_on(led[8]) ,LedStatic |= 0x0008):(lv_led_off(led[8]),LedStatic &= ~0x0008);break;
		case  9:(toggle)?(lv_led_on(led[9]) ,LedStatic |= 0x0020):(lv_led_off(led[9]),LedStatic &= ~0x0020);break;
		case 10:(toggle)?(lv_led_on(led[10]),LedStatic |= 0x0400):(lv_led_off(led[10]),LedStatic &= ~0x0400);break;
		case 11:(toggle)?(lv_led_on(led[11]),LedStatic |= 0x0200):(lv_led_off(led[11]),LedStatic &= ~0x0200);break;
		
	}
}

/***********************************************
*函数名称：BaudSet
*功    能：设置485波特率
*入口参数：lednum：指示灯序号
           toggle：指示灯状态  0熄灭  1点亮
*返 回 值：无
*备    注：
************************************************/
static void BaudSet(void)
{
  static uint16_t bound = 9600;
			
  if(bound != P_Buf[BAUD].newdata){
	  if(P_Buf[BAUD].newdata == 1200 ||P_Buf[BAUD].newdata == 2400 ||
		  P_Buf[BAUD].newdata == 4800 ||P_Buf[BAUD].newdata == 9600){
				
			bound = P_Buf[BAUD].newdata;
			UART_Close(UART0);
			UART_SetBaudrate(UART0,bound);
			UART_Open(UART0);
				
		}
	}
}

/***********************************************
*函数名称：LedSingleInput
*功    能：处理外部输入的LED控制信号
*入口参数：无
*返 回 值：无
*备    注：
************************************************/
static void LedSingleInput(void)
{
	static uint16_t inputflag = 0;                                                //输入信号状态标志位
	
	if((r165 & 0x001F) != inputflag){
		inputflag = r165 & 0x001F;
		if((r165 & 0x0001) == 0){led_toggle(3,1);}                                  //干烧指示灯亮
		else {led_toggle(3,0);}                                                     //干烧指示灯灭
			
		if((r165 & 0x0002) == 0){led_toggle(8,1);}                                  //过载指示灯亮
		else {led_toggle(8,0);}                                                     //过载指示灯灭
		
		if((r165 & 0x0004) == 0){led_toggle(2,1);}                                  //相序指示灯亮
		else {led_toggle(2,0);}                                                     //相序指示灯灭
  }
}






